/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author houfukude
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class JFileChooserDemo implements ActionListener{
    JFrame frame = new JFrame("JFileChooser Demo");
    JFileChooser fc = new JFileChooser();
    JTextField tf = new JTextField();
    JButton oB,sB,dB;

    public static void main(String args[]){
        JFileChooserDemo fcd = new JFileChooserDemo();
        fcd.go();
    }

    public void go(){
        ImageIcon oI = new ImageIcon("open.gif");
        oB = new JButton("Open a File...", oI);
        oB.addActionListener(this);

        ImageIcon sI = new ImageIcon("save.gif");
        sB = new JButton("Save a File...", sI);
        sB.addActionListener(this);

        ImageIcon dI = new ImageIcon("delete.gif");
        dB = new JButton("Delete a File...",dI);
        dB.addActionListener(this);

        JPanel jp = new JPanel();
        jp.add(oB);
        jp.add(sB);
        jp.add(dB);

        Container cp = frame.getContentPane();
        cp.add(jp,BorderLayout.CENTER);
        cp.add(tf,BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setVisible(true);

        }

    public void actionPerformed(ActionEvent e){
        JButton b1 = (JButton)e.getSource();

        if(b1 == oB){
            int select =fc.showOpenDialog(frame);
            if(select == JFileChooser.APPROVE_OPTION){
                File file = fc.getSelectedFile();
                tf.setText("Opening:" + file.getName());
            }else{
                tf.setText("Open command cancelled by user");
            }
        }

        if(b1 == sB){
            int select = fc.showSaveDialog(frame);
            if(select == JFileChooser.APPROVE_OPTION){
                File file = fc.getSelectedFile();
                tf.setText("Saving:" + file.getName());
            }else{
                tf.setText("Save command cancelled by user");
            }
        }

        if(b1 == dB){
            int select = fc.showDialog(frame,"删除嘛");
            if(select == JFileChooser.APPROVE_OPTION){
                File file = fc.getSelectedFile();
                tf.setText("Deleteing:" + file.getName());
            }else{
                tf.setText("Delete command cancelled by user");
            }
        }
    }

}
